import React, {Component} from 'react';
import {Form, Input, Layout, Button} from 'antd';
import '../css/login.css';
import phones from '../image/phones.png';
import logo from '../image/logo.jpg';
import erweima from '../image/yiyiquerweima.png';
import {login, requestUrl} from "../value";
import app from '../AppMethod';

const FormItem = Form.Item;

const {Content} = Layout;

//登录界面
class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {
            admin_username: '',
            admin_password: '',
            errorMsg: '',
            inputTarget: '',
            isValid: true,
            loading: false,
        }
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const {name,dispatch}=this.props;
        return (
            <Layout style={{background: '#fff', height: '100%', textAlign: 'center', verticalAlign: 'middle'}}>
                <Content style={{background: '#fff', minHeight: 600}} className="m-margin-top-xs">
                    <div className="header">
                        <div className="area">
                            {/*<a href="" target="_blank"><img className="logo" src={logo}/></a>*/}
                            <div className="greeting">欢迎登录</div>
                            {/*<div className="menu">*/}
                                {/*<a href="#">企业微信<img className="erweima" src={erweima}/></a>*/}
                            {/*</div>*/}
                        </div>
                    </div>
                    <div className="contents">
                        <div className="bg"></div>
                        <div className="area">
                            <div className="phones_img">
                                {/*<img src={phones}/>*/}
                                <div className="contents_text">

                                </div>
                            </div>
                            <div className="loginbox">
                                <h1>平台后台管理系统</h1>
                                <Form className="login-form">
                                    <div className="item userName">
                                        <label></label>
                                        <FormItem>
                                            {
                                                getFieldDecorator('admin_username', {
                                                    rules: [{required: true, message: '请输入账号！'}],
                                                })(
                                                    <Input id="txt_userName" className="empty" type='text'
                                                           placeholder="输入管理员账号"/>
                                                )
                                            }
                                        </FormItem>
                                    </div>
                                    <div className="item passWord">
                                        <label></label>
                                        <FormItem>
                                            {
                                                getFieldDecorator('admin_password', {
                                                    rules: [{required: true, message: '请输入密码！'}],
                                                })(
                                                    <Input id='txt_password' type='password' className='empty'
                                                           placeholder="请输入密码"/>
                                                )
                                            }
                                        </FormItem>
                                    </div>
                                    <Button
                                        loading={this.state.loading}
                                        onClick={() => this.handleSubmit()}
                                        id="loginSubmit" className='submit'
                                    >
                                        登 录
                                    </Button>
                                </Form>
                            </div>
                        </div>
                    </div>

                    <div className="bottom">
                        <div className="area info">
                            <span>技术支持：领蓝信息科技有限公司</span>
                        </div>
                    </div>
                </Content>
            </Layout>
        )
    };

    componentWillUnmount() {
        this.setState = (count, callback) => {
            return;
        }
    }

    /**
     * 处理登录按钮事件
     */
    handleSubmit() {
        let _this = this;
        this.props.form.validateFields((error, values) => {
            if (!error) {
                this.setState({loading: true});
                app.post(login.TO_LOGIN, values).then((result) => {
                    if (result.code === 1) {
                        let token = result.data.token;
                        app.setToken(token);
                        let menuData = JSON.stringify(result.data.menu);
                        app.setData("menu", menuData);
                        app.setData("username", result.data.admin_username);
                        _this.setState({loading: false});
                        app.setData('selectKey','/index/orderManage');
                        this.props.history.push("/index/orderManage");
                    } else {
                        _this.setState({loading: false});
                        app.alert(result.msg, 'error', 2);
                    }
                    this.setState({loading: false});
                }, (error) => {
                    _this.setState({loading: false});
                }).catch((err) => {
                    _this.setState({loading: false});
                })
            }
        });
    }
}

const Logins = Form.create()(Login);
export default Logins;